package com.gxa.mobile.controller;

import com.gxa.service.GoodsRpcVo;
import com.gxa.core.result.Result;
import com.gxa.core.result.ResultUtils;
import com.gxa.mobile.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <h3>coffee</h3>
 * <p></p>
 *
 * @author : ch
 * @date : 2024-03-22 11:30
 **/
@RestController
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 向购物车中添加商品
     * @param
     * @return
     */
    @PostMapping("/Cart/addItem")
    public Result addItem(Integer userId, Integer goodsId, Integer number){

        if (shoppingCarService.addItem(userId,goodsId,number)){
            System.out.println("加购成功");
        }
        return ResultUtils.buildSuccess("加购成功");

    }

    /**
     * 用token数据来查询购物车(不分页)
     * @param
     * @return
     */
    @PostMapping("/Cart/selectCart")
    public Result selectAllCart(String userId){
        return ResultUtils.buildSuccess(shoppingCarService.selectCart(userId));
    }

    /****************************************************************/



    /**
     * 清空用户购物车
     * @param
     * @return
     */
    @PostMapping("/Cart/deleteUserCart")
    public Result deleteUserCart(Integer userId){

        return ResultUtils.buildSuccess(shoppingCarService.deleteUserCart(userId));
    }

    /**
     * 给购物车删除商品
     * @param
     * @return
     */
    @PostMapping("/Cart/deleteCart")
    public Result deleteCart(Integer userId,Integer goodsId){

        return ResultUtils.buildSuccess(shoppingCarService.deleteCart(userId,goodsId));

    }

    /**
     * 结算部分商品
     * @param
     * @return
     */
    @PostMapping("/Cart/buyShoppingAllCart")
    public Result buyShoppingAllCart(Integer userId){

        return ResultUtils.buildSuccess(shoppingCarService.buyShoppingAllCart(userId));

    }

    @PostMapping("/Cart/buyShoppingCart")
    public Result buyShoppingCart(Integer userId, String goodIds) {
        if (userId == null || goodIds == null || goodIds.isEmpty()) {
            return ResultUtils.buildFail("Invalid input");
        }

        String[] idStrings = goodIds.split(",");
        List<Result<GoodsRpcVo>> results = new ArrayList<>();
        for (String goodsId : idStrings) {
            try {
                Integer goodsIdAsInt = Integer.parseInt(goodsId.trim());
                Result<GoodsRpcVo> goodsResult = shoppingCarService.buyShoppingCart(userId, goodsIdAsInt);
                results.add(goodsResult);
            } catch (NumberFormatException e) {
                return ResultUtils.buildFail("Invalid goods ID: " + goodsId);
            } catch (Exception e) {
                // 处理shoppingCarService.buyShoppingCart可能抛出的其他异常
                return ResultUtils.buildFail("An error occurred while buying goods: " + e.getMessage());
            }
        }
        // 所有商品购买成功
        return ResultUtils.buildSuccess(results);
    }

    /**
     * 给购物车添加商品
     * @param
     * @return
     */
    @PostMapping("/Cart/addCarts/{userId}/{goodsId}/{number}{token}")
    public Result addCarts(Integer userId, Integer goodsId, Integer number){

        return ResultUtils.buildSuccess(shoppingCarService.addItem(userId,goodsId,number));
    }

    //根据quantity更新商品数量，quantity的值为1/-1
    @PostMapping("/Cart/updateCartItemQuantity")
    public Result updateCartItemQuantity(@RequestParam("userId") Integer userId,
                                         @RequestParam("goodsId") Integer goodsId,
                                         @RequestParam("quantity") Integer quantity){
        // 验证quantity的值，确保它是1或-1
        if (quantity != 1 && quantity != -1) {
            return ResultUtils.buildFail("Invalid quantity value. Only 1 or -1 are allowed.");
        }
        return ResultUtils.buildSuccess(shoppingCarService.reduceCartItemQuantity(userId,goodsId,quantity));

    }

    //根据number更新商品数量，number>=0
    @PostMapping("/Cart/updateCart")
    public Result updateCart(Integer userId, Integer goodsId, Integer number){

        // 验证quantity的值，确保它是1或-1
        if (number<0) {
            return ResultUtils.buildFail("Invalid quantity value. Only 1 or -1 are allowed.");
        }
        return ResultUtils.buildSuccess(shoppingCarService.updateCart(userId,goodsId,number));

    }
}
